package com.fattyCorps.battle.service.battle.behavior.state;

import com.fattyCorps.battle.service.battle.Battle;
import com.fattyCorps.battle.service.battle.behavior.Behavior;
import com.fattyCorps.battle.service.battle.behavior.BhvBlackboard;
import com.fattyCorps.battle.service.battle.behavior.TargetSelector;
import com.fattyCorps.battle.service.battle.data.BattleHero;
import com.fattyCorps.common.obj.config.SkillBehaviorMould;
import com.fattyCorps.common.util.fsm.FsmState;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

// 确定目标
@Slf4j
public class SSelectTgt extends FsmState {
    @Override
    public void onEnter() {
        SkillBehaviorMould cfg = (SkillBehaviorMould)getBlackboard(BhvBlackboard.BHV_CFG);
        Battle battle = (Battle)getBlackboard(BhvBlackboard.BATTLE);
        BattleHero owner = (BattleHero)getBlackboard(BhvBlackboard.BHV_OWNER);
        Behavior bhv = (Behavior)getBlackboard(BhvBlackboard.BHV);

        // 收集对目标造成的结果，最后对所有结果做统一伤害结算
        Integer fBhvId = cfg.getTargetFollowBehaviorId();
        if (fBhvId != null && fBhvId > 0) {
            List<BattleHero> fHeroes = battle.getBhvProcessor().getBhvTarget().getTargets(fBhvId);
            if (fHeroes == null) {
                throw new RuntimeException("没有找到跟随目标");
            }

            battle.getBhvProcessor().getBhvTarget().addAll(bhv, fHeroes);
        } else {
            List<BattleHero> select = TargetSelector.select(battle, owner, cfg.getTargetType());
            battle.getBhvProcessor().getBhvTarget().addAll(bhv, select);
        }

        getMch().setState(SEffectFilter.class);
    }
}
